package com.mairuide.bill.treeview;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mairuide._frame.base.BaseController;
import com.mairuide._frame.utils.AccountNode;
import com.mairuide.bill.DealFlow;
import com.mairuide.bill.DealFlowService;
import com.mairuide.bill.information.PersonInformation;
import com.mairuide.bill.information.PersonInformationService;

/**
 * 获取案件关联的人员和卡号信息
 * @author songRT
 */
@Controller
@RequestMapping("/bill")
public class BillTreeController extends BaseController {
	
	@Autowired
	private BillTreeService billTreeService;
	
	@Autowired
	private DealFlowService dealFlowService;
	
	@Autowired
	private PersonInformationService personInformationService;
	
	
	/**
	 * 获取案件下面的资金账户信息
	 * @param model
	 * @return
	 */
	@RequestMapping("/getAccountTree")
	@ResponseBody
	public Object getAccountTree(HttpServletRequest request,ModelMap model) {
		String caseId = request.getSession().getAttribute("caseId") == null?"":request.getSession().getAttribute("caseId").toString();
		List<AccountNode> trees = new ArrayList<AccountNode>();
		if(StringUtils.isNotEmpty(caseId)) {
			trees = billTreeService.getAccountTree(caseId);
		}else {
			AccountNode node = new AccountNode();
			node.setNodeId("0");
			node.setText("交易主体名单");
			trees.add(node);
		}
		return SUCCESS(trees);
	}
	
	/**
	 * 获取案件下面的资金账户信息
	 * @param model
	 * @return
	 */
	@RequestMapping("/getAccountTree2")
	@ResponseBody
	public Object getAccountTree2(HttpServletRequest request,ModelMap model,String caseId) {
		List<BillTreeNode> trees = new ArrayList<BillTreeNode>();
		if(StringUtils.isNotEmpty(caseId)) {
			trees = billTreeService.getAccountTree2(caseId);
		}else {
			BillTreeNode rootNode = new BillTreeNode();
			rootNode.setId("pid_0");//根节点
			rootNode.setName("交易主体名单");
			rootNode.setTitle("交易主体名单");
			rootNode.setPId("pid_-1");
			rootNode.setOpen(true);
			rootNode.setIsParent(true);
			trees.add(rootNode);
		}
		return SUCCESS(trees);
	}
	
	/**
	 * 编辑交易主体跳转
	 * @param nodeText
	 * @return
	 */
	@RequestMapping("/editAccBodyInit")
	public String editAccBodyInit(HttpServletRequest request,ModelMap model, String nodeText,String cardNumbers) {
		String caseId = request.getSession().getAttribute("caseId") == null?"":request.getSession().getAttribute("caseId").toString();
		DealFlow df = dealFlowService.findFirstByAccountBody(nodeText,caseId);
		if(null != df) {
			model.addAttribute("idNumber", df.getIdNumber());
			model.addAttribute("personType", df.getBodyType());
		}else {
			model.addAttribute("idNumber", "");
			model.addAttribute("personType", "ceshi");
		}
		model.addAttribute("cardNumbers", cardNumbers);
		model.addAttribute("nodeText", nodeText);
		return "/account/accBodyEdit";
	}
	
	/**
	 * 删除交易主体跳转
	 * @param nodeText
	 * @return
	 */
	@RequestMapping("/deleteAccBodyInit")
	public String deleteAccBodyInit(ModelMap model, String nodeText,String cardNumbers) {
		model.addAttribute("nodeText", nodeText);
		model.addAttribute("cardNumbers", cardNumbers);
		return "/account/accBodyDelete";
	}
	
	/**
	 * 修改交易主体名称、身份证号
	 * @param model
	 * @param accountBody
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/editAccBody", method=RequestMethod.POST)
	public Object editAccBody(ModelMap model, String nodeText,String personType, String oldNodeText,String idNumber, String cardNumbers) throws Exception {
		String caseId = request.getSession().getAttribute("caseId") == null ? "" : request.getSession().getAttribute("caseId").toString();
		if(com.mairuide._frame.utils.StringUtils.isNotEmpty(cardNumbers)) {
			List<String> accountBodyCards = Arrays.asList(cardNumbers.split(","));
			dealFlowService.findByAccountBodyCardAndCasemanageId(accountBodyCards, caseId,nodeText,idNumber,personType);
		}else {
			dealFlowService.findByAccountBodyAndCasemanageId(oldNodeText, caseId,nodeText,idNumber,personType);
		}
		//查询人员表是否存在修改的人，如果存在则更新，如果不存在则插入
		PersonInformation person = personInformationService.findByPersonName(oldNodeText);
		if(person == null) {
			PersonInformation p = new PersonInformation();
			p.setPersonName(nodeText);
			p.setIdNumber(idNumber);
			p.setBodyType(personType);     
			p.setCasemanagerId(caseId);
			p.setDataSource("左侧菜单手动录入");
			personInformationService.save(p);
		}else {
			person.setPersonName(nodeText);
			person.setIdNumber(idNumber);
			person.setBodyType(personType);
			person.setCasemanagerId(caseId);
			person.setDataSource("左侧菜单手动修改");
			personInformationService.save(person);
		}
		return SUCCESS();
	}
	
	/**
	 * 验证交易主体名称是否重复
	 * @param accountBody
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/verifyAccountBodyUnique", method = RequestMethod.POST)
	public Object verifyCompanyNameUnique(@RequestParam(defaultValue="") String accountBody) {
		if (StringUtils.isEmpty(accountBody)) {
			return ERROR("empty");
		} else {
			String caseId = request.getSession().getAttribute("caseId") == null ? "" : request.getSession().getAttribute("caseId").toString();
			List<DealFlow> dfs = dealFlowService.findByAccountBodyAndCasemanageId(accountBody, caseId);
			if(null != dfs && dfs.size() > 0) {
				return SUCCESS();
			}else {
				return ERROR("repeat");
			}
		}
	}
	
}
